package com.atom.module.document.utils;

import android.util.Size;


import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

/**
 * All rights Reserved, Designed By www.rongdasoft.com
 *
 * @version V1.0
 * @Title: ListUtils
 * @Description:
 * @author: wuyongzheng
 * @date: 2022/3/25
 * @Copyright: 2022/3/25 www.rongdasoft.com Inc. All rights reserved.
 */
public class ListUtils {

    public static Size minByOrNull(List<Size> sizes) {
        Collections.sort(sizes, (o1, o2) -> (o1.getWidth() * o1.getHeight()) - (o2.getWidth() * o2.getHeight()));
        return sizes.get(0);
    }

    public static Size maxByOrNull(List<Size> sizes) {
        Collections.sort(sizes, (o1, o2) -> (o2.getWidth() * o2.getHeight()) - (o1.getWidth() * o1.getHeight()));
        return sizes.get(0);
    }

    public interface Filter<T> {
        boolean isFilter(T t);
    }

    public static <T> List<T> filter(List<T> srcList, Filter<T> filter) {
        List<T> result = new ArrayList<>(srcList);
        Iterator<T> iterator = result.iterator();
        while (iterator.hasNext()) {
            if (filter.isFilter(iterator.next())) {
                iterator.remove();
            }
        }
        return result;
    }
}
